(function(){
    var _this=this;
    return {
        init:function(){
            _this.autoReload=false;//禁止监听数据自动刷新页面
            _this.data.sysUser=window.sysUser;
            _this.data.sysUser.firstName=window.sysUser.name.substring(0,1);
            var data=window.sysUser.sysPermission;
            _this.data.menuList=[];
            _this.data.sysPermissisonsMap = {};
            _this.data.sysPermissisonsIdMap = {};
            var selfPermissionCode=[];
            for(var i=0;i<data.length;i++){
                selfPermissionCode.push("."+data[i].code);
                data[i].href=data[i].path;
                if(data[i].type==1){
                    _this.data.menuList.push(data[i]);
                }
                _this.data.sysPermissisonsMap[data[i].path] = data[i];
                _this.data.sysPermissisonsIdMap[data[i].id] = data[i];
            }
            _this.data.menu=_this.toTree([],null);
            writeStyle(selfPermissionCode.join(",")+"{display:inline-block!important}");
            _this.selected();
            _this.reload();
        },
        data:{
            sysUser: {},
            menuList:[],
            sysPermissisonsMap:{},
            sysPermissisonsIdMap:{},
            menu:{}
        },
        before:function(data){
            return data;
        },
        after:function(){
            _this.$(".e-modify-password").click(function(){
                DsModule.to("module/sysUser/modifyPassword");
            })
            $("#side-menu").metisMenu();
            this.$(".J_menuItem").click(function(e){
                e.preventDefault();
                var href=$(this).attr("href");
                $(".clear-active").removeClass("active");
                if($(this).parent().hasClass("clear-active")){
                    $(this).parent().addClass("active");
                }
                if( $(this).parent().parent().parent().hasClass("clear-active")){
                    $(this).parent().parent().parent().addClass("active");
                }
                if( $(this).parent().parent().parent().parent().parent().hasClass("clear-active")){
                    $(this).parent().parent().parent().parent().parent().addClass("active");
                }
                if($("body").hasClass("mini-navbar")){
                    $("body").toggleClass("mini-navbar");
                    SmoothlyMenu();
                }
                DsModule.to(href);

            });
            //菜单滚动条
            $(".sidebar-collapse").slimScroll({height: "100%", railOpacity: .9, alwaysVisible: !1});
            $("#side-menu>li li a").click(function () {
                $(window).width() < 769 && NavToggle()
            });
            $(".parent_menu>i").click(function () {
                $(window).width() < 769 && NavToggle()
            });
            $(".nav-close").click(NavToggle);
            if(!$("body").hasClass("body-small")){
                var e = $("body > #wrapper").height() - 61;
                $(".sidebard-panel").css("min-height", e + "px")
            }
        },
        selected:function(){
            var data=this.data.menu;
            var href =this.params.href;
            if(href){
                var permission = _this.data.sysPermissisonsMap[href];
                if(permission!=null){
                    if(permission.type==0){
                        for(;;){
                            permission=_this.data.sysPermissisonsIdMap[permission.parentId];
                            if(permission.type==1){
                                break;
                            }
                        }
                    }
                }
            }
            href=permission!=null?permission.href:"";
            var forSelected=function(data1){
                var flag=false;
                for(var m in data1){
                    if(data1[m].menu!=null){
                        flag= forSelected(data1[m].menu);
                        if(flag){
                            data1[m].class="clear-active active";
                        }else{
                            data1[m].class="clear-active";
                        }
                    }else{
                        if(data1[m].href==href&&href!=""){
                            flag=true;
                            data1[m].class="clear-active active";
                        }else{
                            data1[m].class="clear-active";
                        }
                    }
                }
                return flag;
            };
            forSelected(data);
        },
        toTree:function(treeData,parentId){
            var data=_this.data.menuList;
            for(var i=0;i<data.length;i++){
                if(data[i].parentId==parentId){
                    var children=_this.toTree([],data[i].id);
                    if(children!=null&&children.length>0){
                        data[i].menu=children;
                    }
                    treeData.push(data[i]);
                }
            }
            return treeData;
        },
    }
});